package com.arquitectonic.client.interfaz.ventanas;

import com.arquitectonic.client.datosYlogica.GestorStakeholders;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.MultipleAppearance;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class VentanaNuevoStakeholder extends Window {
	public static VentanaNuevoStakeholder getInstance() {
		if (singleton == null) {
			singleton = new VentanaNuevoStakeholder();
		}
		return singleton;
	}

	static private VentanaNuevoStakeholder singleton = null;
	static private DynamicForm form = null;

	private VentanaNuevoStakeholder() {
		// parametros de la ventana:
		this.setAutoSize(true);
		this.setTitle("Agregar nuevo stakeholder");
		this.setLeft(400);
		this.setTop(400);
		this.setCanDragReposition(true);
		this.setIsModal(true);
		this.setShowModalMask(true);



		// elementos del formulario:
		form = new DynamicForm();

		// Campo para los roles
		SelectItem roles = new SelectItem("roles","Rol/es");
		roles.setMultiple(true);
		roles.setMultipleAppearance(MultipleAppearance.PICKLIST);
		roles.setValueMap(GestorStakeholders.rolesPosibles);
		
		form.setFields(new FormItem[] {
				new TextItem("nombreStakeholder", "Nombre"),
				new TextItem("prioridad", "Prioridad"),
				new TextItem("cantidad", "Cantidad"),
				roles
		});

		// boton de agregar:
		IButton ok = new IButton("OK");
		ok.setAutoFit(true);
		ok.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				try {
					String name = (String) (form.getField("nombreStakeholder")
							.getValue());
					if (name == null || name.equals(""))
						throw new RuntimeException(
								"nombre del stakeholder no definido");
					double priority = Double.valueOf((String) (form
							.getField("prioridad").getValue()));
					if (priority < 0.0 || priority > 1.0)
						throw new RuntimeException(
								"La prioridad debe estar definida entre 0 y 1");
					int count = Integer.valueOf((String) (form
							.getField("cantidad").getValue()));
					if (count <= 0)
						throw new RuntimeException(
								"La cantidad de stakeholders debe ser mayor que 0");

					try {
						GestorStakeholders.agregarStakeholder(form
								.getValuesAsRecord());
					} catch (Exception e) {
						SC.warn(e.getMessage());
					}
					VentanaNuevoStakeholder.getInstance().hide();

				} catch (RuntimeException e) {
					SC.warn(e.getMessage());
				}

			}
		});
		ok.setLayoutAlign(Alignment.RIGHT);

		// agregando componentes a la ventana:
		VLayout layout = new VLayout();
		layout.addMember(form);
		layout.addMember(ok);
		this.addItem(layout);

		// Centramos la ventana:
		this.centerInPage();

	}
}